package com.twetec.cms.adapter.pc.backend.config;


import com.google.common.collect.Maps;

import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.FileResourceLoader;
import org.beetl.core.tag.TagFactory;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;

import java.util.Map;

@Configuration
public class BeetlConfiguration {


    /**
     * 模板存储资源文件根路径
     */
    @Value("${twetec.resource.storage-root-path}")
    private String fileResourceLoaderPath;

    /**
     * 设置全局属性
     * @return
     */
    public Map<String,Object> sharedVars(){
        Map<String,Object> sharedVars =  Maps.newHashMap();

        // 全局的变量
        sharedVars.put("helloWorld", "HelloWorld");

        return sharedVars;
    }

    /**
     * 初始化配置
     * @param tagFactory
     * @return
     */
    @Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration beetlGroupUtilConfiguration(@Qualifier("tagFactorys") Map<String, TagFactory> tagFactorys) {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
        try {
            FileResourceLoader fileResourceLoader = new FileResourceLoader(fileResourceLoaderPath, "UTF-8");

            beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
            beetlGroupUtilConfiguration.setResourceLoader(fileResourceLoader);
            beetlGroupUtilConfiguration.setSharedVars(sharedVars());

            // 全局函数
//            beetlGroupUtilConfiguration.setFunctions(function);

            // 自定义模板
            beetlGroupUtilConfiguration.setTagFactorys(tagFactorys);

            // 异常信息
//            beetlGroupUtilConfiguration.setErrorHandler(new TagExceptionHandler());

            return beetlGroupUtilConfiguration;

        } catch (Exception e) {

            throw new RuntimeException(e);

        }
    }

    /**
     * 视图解析
     * @param beetlGroupUtilConfiguration
     * @return
     */
    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier(value = "beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }

    /**
     * 获取模板
     * @param beetlGroupUtilConfiguration
     * @return
     */
    @Bean(name = "groupTemplate")
    public GroupTemplate getGroupTemplate(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        return beetlGroupUtilConfiguration.getGroupTemplate();

    }

}
